package com.wlz.kernel.netty.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.CountDownLatch;

public class NettyClientHandler extends ChannelInboundHandlerAdapter {
    private CountDownLatch lathc;
    /**
     * 客户端读到的返回值
     */
    private Object result;

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
    }

    /**
     * 服务端返回数据触发该方法
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        result=msg;
        lathc.countDown();
        System.out.println("返回数据读取完毕");
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }

    public Object getResult() {
        return result;
    }

    public NettyClientHandler(CountDownLatch lathc) {
        this.lathc = lathc;
    }

    public void reLathc(CountDownLatch lathc){
        this.lathc=lathc;
    }
}
